﻿#include "dataform.h"
#include "ui_dataform.h"
#include <QGraphicsDropShadowEffect>
#include <QListView>
#include <QMessageBox>
#include <Dlg/batchdlg.h>

DataForm::DataForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DataForm)
{
    ui->setupUi(this);

    ui->Bt_Return->hide();

    m_bDB = false;

    this->setAttribute(Qt::WA_DeleteOnClose); // 关闭时候自动释放
    this->setWindowModality(Qt::ApplicationModal); // 只能创建一个

    // 历史查询界面
    // 初始化历史界面
    m_pHistroyDataForm = new WorkHisForm(this);


    ui->Bt_Add->hide();
    ui->Bt_Confirm->hide();


    ui->tableWidget->setShowGrid(true);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    //自动调整窗口列表的【宽、高】---适应窗口大小
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->tableWidget->verticalHeader()->hide();//取消默认行号

    QStringList header;
    header << tr("id")<< tr("batch_name")
           << tr("plan_name") << tr("user_name")
           << tr("createtime") << tr("changetime");
    ui->tableWidget->setColumnCount(header.size());
    ui->tableWidget->setHorizontalHeaderLabels(header);

    m_nCurPage = 1;
    m_nRowsPerPage = 7;
    m_nTotalPages = 1;


    ui->Bt_Search->setEnabled(false);
    ui->Bt_Delete->setEnabled(false);


    connect(ui->tableWidget->verticalScrollBar(), SIGNAL(valueChanged(int)),this,SLOT(OnSlideAction(int)));//响应滚动条翻页
    QFile file(":/qss/dlg.qss");
    if (file.open(QFile::ReadOnly))
    {
        QTextStream filetext(&file);
        QString stylesheet = filetext.readAll();
        this->setStyleSheet(stylesheet);
        file.close();
    }


    QGraphicsDropShadowEffect *shadow_effectA = new QGraphicsDropShadowEffect(this);
    QGraphicsDropShadowEffect *shadow_effectB = new QGraphicsDropShadowEffect(this);
    QGraphicsDropShadowEffect *shadow_effectC = new QGraphicsDropShadowEffect(this);
    shadow_effectA->setOffset(0, 0);
    shadow_effectA->setColor(QColor(150,150,150));
    shadow_effectA->setBlurRadius(6);

    shadow_effectB->setOffset(0, 0);
    shadow_effectB->setColor(QColor(150,150,150));
    shadow_effectB->setBlurRadius(6);

    shadow_effectC->setOffset(0, 0);
    shadow_effectC->setColor(QColor(150,150,150));
    shadow_effectC->setBlurRadius(6);


    ui->Data_gBox_1->setGraphicsEffect(shadow_effectA);
    ui->Data_gBox_2->setGraphicsEffect(shadow_effectB);
    ui->Data_gBox_3->setGraphicsEffect(shadow_effectC);

    //允许绘制背景色交替 必须写这句否则交替背景色没有效果
    ui->tableWidget->setAlternatingRowColors(true);

    ui->plan_cBox->setView(new  QListView());
    ui->customer_cBox->setView(new  QListView());
}

DataForm::~DataForm()
{
    delete ui;
}

void DataForm::setBatchPage()
{

    ui->Bt_Add->show();
    ui->Bt_Confirm->show();
    ui->Bt_Confirm->setEnabled(false);
    ui->Bt_Delete->hide();
    ui->Bt_Return->hide();
    on_Bt_Query_clicked();
    iniPage();
}

void DataForm::iniPage()
{
    ui->customer_cBox->clear();
    ui->plan_cBox->clear();
    SqliteUtil m_sql;
    m_sql.init("BatchCreate_db");
    QList<T_PlanLine> plan_recoders = m_sql.queryAllPlanLines();
    foreach(T_PlanLine recoder,plan_recoders)
    {
        ui->plan_cBox->addItem(recoder.planName,recoder.id);
    }


    QList<T_Batch> recoders =  m_sql.queryAllBatchs();
    ui->tableWidget->clearContents();
    if(recoders.size()%m_nRowsPerPage ==0){
        m_nTotalPages = recoders.size()/m_nRowsPerPage;
    }else{
        m_nTotalPages = recoders.size()/m_nRowsPerPage+1;
    }

    ui->labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));

    ui->tableWidget->setRowCount(recoders.size()); // 设置显示的行数

    for(int i=0;i<recoders.size();i++){
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(recoders[i].id)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(recoders[i].batch_name));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(recoders[i].plan_name));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(recoders[i].user_name));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(recoders[i].create_time));
        ui->tableWidget->setItem(i,5,new QTableWidgetItem(recoders[i].change_time));


        for(int j=0;j<6;j++){
            ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        }
    }

}


void DataForm::OnSlideAction(int nRowIndex)
{
    ++nRowIndex;
    //计算当前行是那一页
    if (nRowIndex % m_nRowsPerPage == 0)
    {
        m_nCurPage = nRowIndex / m_nRowsPerPage;
    }
    else
    {
        m_nCurPage = nRowIndex / m_nRowsPerPage+1;
    }
    ui->labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));
}

void DataForm::slotsDlgInfo()
{
    iniPage();
}



void DataForm::on_Bt_PrePage_clicked()
{
    m_nCurPage--;
    if(m_nCurPage <=0){
        m_nCurPage = m_nTotalPages;
    }
    ui->tableWidget->verticalScrollBar()->setSliderPosition(m_nCurPage*m_nRowsPerPage - m_nRowsPerPage);
    ui->labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));
}

void DataForm::on_Bt_NextPage_clicked()
{
    m_nCurPage++;
    if (m_nCurPage > m_nTotalPages)
    {
        m_nCurPage = 1;
    }

    ui->tableWidget->verticalScrollBar()->setSliderPosition(m_nCurPage*m_nRowsPerPage - m_nRowsPerPage);
    ui->labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));
}

void DataForm::on_Bt_Delete_clicked()
{
    if (!(QMessageBox::question(this, tr("Delete"), tr("Whether to delete the current row?"), tr("Yes"), tr("No"))))
    {

    }
}

void DataForm::on_tableWidget_cellClicked(int row, int column)
{
    column = 0;
    if(m_strCurID.isEmpty()){
        ui->Bt_Search->setEnabled(true);
        ui->Bt_Delete->setEnabled(true);
        ui->Bt_Confirm->setEnabled(true);
    }
    QTableWidgetItem* item = ui->tableWidget->item(row ,0);
    m_strCurID = item->text();
}


void DataForm::on_Bt_Confirm_clicked()
{
    qDebug() << m_strCurID.toInt();
    emit sendBatchInfo(m_strCurID.toInt());
    this->close();
}

void DataForm::on_Bt_Return_clicked()
{

}

void DataForm::on_tableWidget_cellDoubleClicked(int row, int column)
{
    // column = row;
    // on_Bt_Search_clicked();
    if(QMessageBox::Yes != QMessageBox::information(this,tr("remind"),tr("Whether to view historical data."),QMessageBox::Yes | QMessageBox::No))
        return;

    int batchID = ui->tableWidget->item(row,0)->text().toInt();   // 获取批次ID
    QString batchName = ui->tableWidget->item(row,1)->text();   // 获取批次名
    QString customerName = ui->tableWidget->item(row,3)->text();   // 获取客户名

    m_pHistroyDataForm->setBatchIDName(batchID, batchName);
    m_pHistroyDataForm->setCustomerName(customerName);
    m_pHistroyDataForm->clearTableWidget();
    m_pHistroyDataForm->updateHistoryData();
    m_pHistroyDataForm->exec();

}

void DataForm::on_plan_cBox_activated(int index)
{
    m_nPlanId = index;
}

void DataForm::on_customer_cBox_activated(int index)
{
    m_nCustomerId = index;
}

void DataForm::on_Bt_Query_clicked()
{

}

void DataForm::on_Bt_Search_clicked()
{

}

void DataForm::on_Bt_Add_clicked()
{
    BatchDlg* m_pCreate = new BatchDlg();
    m_pCreate->show();
    connect(m_pCreate,&BatchDlg::sigFormInfo,this,&DataForm::slotsDlgInfo);
}
